package com.prs.crm.action.pm;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.w3c.dom.Document;

import com.prs.crm.action.XMLExportAction;
import com.prs.crm.annotation.Clew;
import com.prs.crm.context.Constrains;
import com.prs.crm.domain.pm.PmProject;
import com.prs.crm.domain.pm.PmRelatStage;
import com.prs.crm.domain.pm.PmStage;
import com.prs.crm.service.pm.PmProjectService;
import com.prs.crm.service.pm.PmStageService;
import com.prs.crm.util.GanttExport;
import com.prs.crm.vo.gantt.GanttTask;
import com.prs.crm.vo.gantt.PredecessorTask;

public class GanttAction extends XMLExportAction{
	private PmProject pmProject;
	private PmProjectService pmProjectService;
	private PmStageService pmStageService;
	private Document document;	
	private GanttExport ganttExportUtil;	

	
	@Action("ganttView")
	public String ganttView() {
		return SUCCESS;
	}
	
	@Action("activateProject")
	public String activateProject() {
		return SUCCESS;
	}
	
	@Clew(results={"success"})
	@Action(value="submitActivateProject",results={@Result(name="success",type="redirect",location="listInitProject")})
	public String submitActivateProject() {
		pmProject.setState(Constrains.PM_STATE_START);
		for(PmStage stage : pmProject.getStages()) {
			stage.setState(Constrains.PM_STATE_START);
			this.getPmStageService().save(stage);
		}
		this.getPmProjectService().save(pmProject);
		this.setPrompt("激活项目成功");
		return SUCCESS;
	}
	
	//加载甘特图数据
	@Action(value="loadGanttData",results={@Result(name="success",type="xslt",params={"exposedValue","document","location","/WEB-INF/xml.xsl"})})
	public String loadGanttData() throws Exception{
		
		GanttTask root = new GanttTask(0,pmProject.getTitle());
		root.setStart(pmProject.getStartTime());
		root.setFinish(pmProject.getEndTime());
		root.setSummary(true);
		
		GanttTask projectRoot = new GanttTask(pmProject.getId(),pmProject.getTitle());
		projectRoot.setStart(pmProject.getStartTime());
		projectRoot.setFinish(pmProject.getEndTime());
		projectRoot.setSummary(true);
		root.addChild(projectRoot);
		
		Collection<PmStage> stages = this.getPmStageService().getRootStage(pmProject);
		for(PmStage stage : stages) {
			GanttTask stageTask = new GanttTask(stage.getId(),stage.getTitle());
			stageTask.setStart(stage.getStartTime());
			stageTask.setFinish(stage.getEndTime());
			if(stage.getChildren().size() != 0) {
				stageTask.setSummary(true);
			}
			List<PredecessorTask> predecessorTasks = new ArrayList<PredecessorTask>();
			for(PmRelatStage relat : stage.getRelatStages()) {
				Integer flag = -1;
				if(relat.getRelation().equals(Constrains.PM_STAGE_FF)) {
					flag = PredecessorTask.TYPE_FF;
				}else if(relat.getRelation().equals(Constrains.PM_STAGE_FS)){
					flag = PredecessorTask.TYPE_FS;
				}else if(relat.getRelation().equals(Constrains.PM_STAGE_SF)) {
					flag = PredecessorTask.TYPE_SF;
				}else if(relat.getRelation().equals(Constrains.PM_STAGE_SS)) {
					flag = PredecessorTask.TYPE_SS;
				}
				PredecessorTask pt = new PredecessorTask(relat.getTargetPmStage().getId(),flag);
				predecessorTasks.add(pt);
			}
			stageTask.setPredecessorTasks(predecessorTasks);
			projectRoot.addChild(stageTask);
			addGanttChild(stage,stageTask);			
		}
		
		document = this.ganttExportUtil.exportXML(root);		
		this.prepareExport();
		return SUCCESS;
	}
	
	
	//递归添加甘特图节点
	private void addGanttChild(PmStage parent,GanttTask parentTask) {
		if(parent.getChildren() != null) {
			for(PmStage stage:parent.getChildren()) {
				GanttTask stageTask = new GanttTask(stage.getId(),stage.getTitle());
				stageTask.setStart(stage.getStartTime());
				stageTask.setFinish(stage.getEndTime());
				if(stage.getChildren().size() != 0) {
					stageTask.setSummary(true);
				}
				List<PredecessorTask> predecessorTasks = new ArrayList<PredecessorTask>();
				for(PmRelatStage relat : stage.getRelatStages()) {
					Integer flag = -1;
					if(relat.getRelation().equals(Constrains.PM_STAGE_FF)) {
						flag = PredecessorTask.TYPE_FF;
					}else if(relat.getRelation().equals(Constrains.PM_STAGE_FS)){
						flag = PredecessorTask.TYPE_FS;
					}else if(relat.getRelation().equals(Constrains.PM_STAGE_SF)) {
						flag = PredecessorTask.TYPE_SF;
					}else if(relat.getRelation().equals(Constrains.PM_STAGE_SS)) {
						flag = PredecessorTask.TYPE_SS;
					}
					PredecessorTask pt = new PredecessorTask(relat.getTargetPmStage().getId(),flag);
					predecessorTasks.add(pt);
				}
				stageTask.setPredecessorTasks(predecessorTasks);
				parentTask.addChild(stageTask);
				addGanttChild(stage,stageTask);
			}
		}
	}
	
	public PmProject getPmProject() {
		return pmProject;
	}
	public void setPmProject(PmProject pmProject) {
		this.pmProject = pmProject;
	}
	public PmProjectService getPmProjectService() {
		return pmProjectService;
	}
	public void setPmProjectService(PmProjectService pmProjectService) {
		this.pmProjectService = pmProjectService;
	}

	public Document getDocument() {
		return document;
	}

	public void setDocument(Document document) {
		this.document = document;
	}

	public GanttExport getGanttExportUtil() {
		return ganttExportUtil;
	}

	public void setGanttExportUtil(GanttExport ganttExportUtil) {
		this.ganttExportUtil = ganttExportUtil;
	}

	public void setPmStageService(PmStageService pmStageService) {
		this.pmStageService = pmStageService;
	}

	public PmStageService getPmStageService() {
		return pmStageService;
	}


}
